<?php
namespace Ucmaim\Controller\Admin;

class ContentController extends CommonController {

    public function _initialize(){
        parent::_initialize();
    }

    public function Index(){
        $this->Category();
    }

    // 栏目分类
    public function Category(){
        $category = D('ContentCategory');
        $list = $category->getCategoryList();
        $this->assign('list', $list);
        $this->display('Category');
    }

    // Add
    public function AddCategory(){
        $category = D('ContentCategory');

        if( IS_POST ){
            $name = trim(I('post.name'));
            $label = trim(I('post.label'));
            $pid = 0;
            $category->addCategory($pid, $name, $label) ? $this->success('添加成功', U('Admin/Content/Index')) : $this->error('添加失败', U('Admin/Content/Index'));
        }else{
            $this->assign('action', '添加内容栏目');
            $this->display();
        }
    }

    // Edit
    public function EditCategory(){
        $category = D('ContentCategory');
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            $name = $post['name'];
            $label = $post['label'];
            $id   = $post['id'];
            $category->updateCategory($id, $name, $label) ? $this->success('更新成功', U('Admin/Content/Index')) : $this->error('更新失败', U('Admin/Content/Index'));
        }else{
            $id = I('get.id');
            $category = $category->categoryInfo($id);
            if(empty($category)) $this->error('此栏目不存在');
            $this->assign('category', $category);
            $this->assign('action', '修改栏目信息');
            $this->assign('id', $id);
            $this->display('AddCategory');
        }
    }

    //Delete
    public function DelCategory(){
        $category = D('ContentCategory');
        $id = I('get.id');
        $category->deleteCategory($id) ? $this->success('删除成功', U('Admin/Content/Index')) : $this->error('删除失败', U('Admin/Content/Index'));
    }

    //管理内容列表
    public function Content(){
        $get = I('get.');
        $category = D('ContentCategory')->getCategoryList();
        $map = [];
        if( $get['keyword'] ){
            $map['title'] = array('LIKE', "%{$get['keyword']}%");
            $this->assign('keyword', $get['keyword']);
        }

        if( $get['category'] ){
            $map['category'] = $get['category'];
            foreach( $category as $k => $v ){
                if( $get['category'] == $v['id'] ){
                    $category[$k]['selected'] = ' selected = "selected"';
                }
            }
        }
        $list = D('Content')->getList($map);

        $list = gotoPage($list, $this->pageSize);
        $this->assign('list', $list['data']);
        $this->assign('page', $list['page']);
        $this->assign('category', $category);
        $this->display();
    }

    // 添加内容
    public function AddContent(){
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            if($post['title'] === '') $this->error('内容标题不能为空！');
            if(! is_numeric($post['category']) || ! (int)$post['category'] > 0) $this->error('请选择一个分类！');

            $data['title']       = $post['title'];
            $data['category']    = $post['category'];
            $data['description'] = $post['description'];
            $data['content']     = htmlspecialchars($post['content']);
            $data['sort']        = (int)$post['sort'] > 0 ? (int)$post['sort'] : 0;
            $data['create_time'] = $data['update_time'] = time();

            $Content = D('Content');
            $Content->addContent($data) ? $this->success('添加成功', U('Admin/Content/Content')) : $this->error('删除失败', U('Admin/Content/Content'));
        }else{
            //获取分类信息
            $category = D('ContentCategory')->getCategoryList();
            $this->assign('category', $category);
            $this->assign('action', '添加内容');
            $this->display('ContentEdit');
        }
    }

    public function EditContent(){
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            if($post['title'] === '') $this->error('内容标题不能为空！');
            if(! is_numeric($post['category']) || ! (int)$post['category'] > 0) $this->error('请选择一个分类！');
            $data['title']       = $post['title'];
            $data['category']    = $post['category'];
            $data['description'] = $post['description'];
            $data['content']     = htmlspecialchars($post['content']);
            $data['sort']        = (int)$post['sort'] > 0 ? (int)$post['sort'] : 0;
            $data['update_time'] = time();
            $Content = D('Content');
            $Content->updateContent($post['id'], $data) ? $this->success('编辑成功', U('Admin/Content/Content')) : $this->error('编辑失败', U('Admin/Content/Content'));
        }else{
            //获取分类信息
            $id = I('get.id');
            $category = D('ContentCategory')->getCategoryList();
            $content  = D('Content')->contentInfo($id);
            foreach($category as $k => $v){
                if( $v['id'] == $content['category'] ){
                    $category[$k]['selected'] = ' selected = "selected"';
                }
            }
            $this->assign('content', $content);
            $this->assign('category', $category);
            $this->assign('action', '修改内容');
            $this->display('ContentEdit');
        }
    }

    public function DeleteContent(){
        //$id = I('get.id');
        //D('Content')->deleteContent($id) ? $this->success('删除成功', U('Admin/Content/Content')) : $this->error('删除失败', U('Admin/Content/Content'));;
    }

    public function UploadImg(){
        import('Think.Upload');
        $Q = vendor('JSON', 'Public/Kindeditor/php/');
        $config = array(
            'maxSize'       =>  1000000,
            'exts'          =>  array('gif', 'jpg', 'jpeg', 'png', 'bmp'), //允许上传的文件后缀
            'autoSub'       =>  true, //自动子目录保存文件
            'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
            'rootPath'      =>  './Uploads/' . MODULE_NAME . '/', //保存根路径
            'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
            'replace'       =>  false, //存在同名是否覆盖
            'hash'          =>  false, //是否生成hash编码
        );
        $Uploader = new \Think\Upload($config);
        $result = $Uploader->uploadOne($_FILES['imgFile']);
        if( !$result ){
            $msg = $Uploader->getError();
            header('Content-type: text/html; charset=UTF-8');
            $json = new \Services_JSON();
            echo $json->encode(array('error' => 1, 'message' => $msg));
            exit;
        }else{
            $file_url = '/Uploads/Ucmaim/' . $result['savepath'] . $result['savename'];
            header('Content-type: text/html; charset=UTF-8');
            $json = new \Services_JSON();
            echo $json->encode(array('error' => 0, 'url' => $file_url));
            exit();
        }
    }
}
